<?php

namespace app\controller;

use app\model\user as UserModel;
use app\validate\User as UserValidate;
use think\exception\ValidateException;
use think\facade\View;
use think\Request;

class Borrow
{
    public function index()
    {
        return View::fetch('D:\php\phpEnv\www\www.tp.com\view\borrow.html');
    }

    public function registration()
    {
        return View::fetch('D:\php\phpEnv\www\www.tp.com\view\registration.html');
    }

    public function add(Request $request)
    {
//        dd($request->param());
        $data = $request->param();
        //数据验证
        try {
            validate(UserValidate::class)->batch(true)->check($data);
        } catch (ValidateException $exception) {
            return view('view/public/toast.html',[
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/user/create')

            ]);
        }
        //写入数据库
        //新增后返回id
        $id = UserModel::create($data)->getData('id');
        //提示
        return $id ? view('./view/public/toast.html', [
            'infos' => ['恭喜，添加成功！'],
            'url_text' => '去首页',
            'url_path' => url('/')
        ]) : '添加失败！';
    }
}